package com.sunshine.wrongquestionset.room.dao

import androidx.lifecycle.LiveData
import androidx.room.*
import com.sunshine.wrongquestionset.room.entity.AppConfigEntity

/**
 * @author sunshine
 * @date 2021/3/26
 * @see com.sunshine.wrongquestionset.room.entity.AppConfigEntity
 */
@Dao
interface AppConfigDao {
    @Insert(entity = AppConfigEntity::class)
    fun insert(appConfigEntity: AppConfigEntity)

    @Delete(entity = AppConfigEntity::class)
    fun delete(appConfigEntity: AppConfigEntity)

    @Query("SELECT * FROM app_config WHERE id=:id")
    fun select(id: String): LiveData<AppConfigEntity>

    //查询登录用户的个数
    @Query("SELECT COUNT(*) FROM app_config WHERE isLogin=1")
    fun hasLoginUserCount(): Int

    @Query("SELECT id FROM app_config WHERE isLogin=1")
    fun getLoginUserId(): LiveData<String?>

    @Update(entity = AppConfigEntity::class, onConflict = OnConflictStrategy.REPLACE)
    fun updateByEntity(appConfigEntity: AppConfigEntity)

    @Query("UPDATE app_config SET isLogin=:isLogin WHERE id=:userId")
    fun update(userId: String, isLogin: Boolean)

//    //更新当前用户的错题集id
//    @Query("UPDATE app_config SET nowQuestionSetId=:nowQuestionSetId WHERE id=:id")
//    fun updateQuestionSetId(id: String, nowQuestionSetId: Int)
}